import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

extension IndexedMapExtension on Iterable<dynamic> {
  Iterable<T> indexedMap<T>(T Function(int index, dynamic element) mapper) {
    return List.generate(
        this.length, (index) => mapper(index, this.elementAt(index)));
  }
}

extension ListExt on List<Widget> {
  List<Widget> mapIndexed(IndexedWidgetBuilder builder) {
    return List.generate(
        length, (index) => builder(this[index] as BuildContext, index));
  }
}
extension ListSafe<E> on List<E> {

  void addSafe(E? element) {
    if (element != null) {
      this.add(element);
    }
  }
}

extension IntExt on int {
  String formatTimestamp([String format = "yyyy-MM-dd HH:mm:ss"]) {
    // 将时间戳转换为 DateTime 对象
    DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(this);
    // 使用 DateFormat 将 DateTime 对象格式化为文本
    DateFormat dateFormat = DateFormat(format); // 你可以根据需要修改格式
    return dateFormat.format(dateTime);
  }
}

class Util {
  var screenWidth = 0.0;
  var screenHeight = 0.0;

  ThemeData? theme;
  static Util? _instance;

  Util._privateConstructor() {}

  static Util shared() {
    if (_instance == null) {
      _instance = Util._privateConstructor();
    }
    return _instance!;
  }

  static double SCREENWIDTH() {
    return Util.shared().screenWidth;
  }

  static double SCREENHEIGHT() {
    return Util.shared().screenHeight;
  }

  static void showLoading() {
    EasyLoading.show(
      status: '加载中...',
      maskType: EasyLoadingMaskType.black,
    );
  }

  static void dimissLoading() {
    EasyLoading.dismiss();
  }
}
